package cc.orange.adapter;

import android.content.Context;
import android.text.Html;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.URLSpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import cc.orange.R;
import cc.orange.entity.ChatHistoryEntity;
import cc.orange.mainView.WebViewActivity;
import cc.orange.utils.CircleImageView;
import cc.orange.utils.ClicksSpans;
import cc.orange.utils.emojis.AndroidEmoji;
import demo.smart.access.xutlis.util.ZXSharedPrefUtil;


/**
 * Created by CM on 2022/10/25.
 */

public class MsgConSystemAdapter extends BaseQuickAdapter<ChatHistoryEntity.Data.ListEntity, BaseViewHolder> {
    ZXSharedPrefUtil sharedPrefUtil;//自己的头像
    public MsgConSystemAdapter(@Nullable List<ChatHistoryEntity.Data.ListEntity> data) {
        super(R.layout.rc_message_item, data);
        sharedPrefUtil = new ZXSharedPrefUtil();
    }

    private String name,photo,uid;//对方的 姓名 头像 id
    public void setNamePhoto(String name,String photo,String uid){
        this.name = name;
        this.photo = photo;
        this.uid = uid;
    }
    @Override
    protected void convert(@NonNull BaseViewHolder helper, ChatHistoryEntity.Data.ListEntity item) {
        CircleImageView rc_left_portrait = helper.getView(R.id.rc_left_portrait);
        CircleImageView rc_right_portrait = helper.getView(R.id.rc_right_portrait);

        CircleImageView talk_msg_bigimg = helper.getView(R.id.talk_msg_bigimg);//大图

        RelativeLayout rc_content = helper.getView(R.id.rc_content);//
        TextView  talk_msg_content = helper.getView(R.id.talk_msg_content); //消息文本
        TextView  rc_time = helper.getView(R.id.rc_time); //时间

        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(0,20,0,0);
        talk_msg_content.setText("");

        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        rc_content.setBackgroundResource(R.drawable.shape_msg_left);
        talk_msg_content.setTextColor(mContext.getResources().getColor(R.color.black));
        rc_left_portrait.setVisibility(View.VISIBLE);
        rc_right_portrait.setVisibility(View.GONE);
        Glide.with(mContext).load(R.drawable.icon_mi_msg_img1).into(rc_left_portrait);

        rc_content.setLayoutParams(layoutParams);

        //        0不含  1含
        helper.addOnClickListener(R.id.talk_msg_bigimg, R.id.talk_msg_bigimg);
        String content = item.getChattingRecords();
        String replace = content.replace("</p>", "");
        String replace2 = replace.replace("<p>", "");
        if(null != item.getIsHanTp() && null != item.getIsHanWz() &&
                item.getIsHanTp().equals("1") && item.getIsHanWz().equals("1")){//文图都有
            talk_msg_bigimg.setVisibility(View.VISIBLE);
            talk_msg_content.setVisibility(View.VISIBLE);

            Glide.with(mContext).load(item.getPicUrl())
                    .into(talk_msg_bigimg);
            if (replace2.contains("href=")) {
                talk_msg_content.setText(getClickableHtml(mContext, replace2));
                talk_msg_content.setMovementMethod(LinkMovementMethod.getInstance());
            } else {
                talk_msg_content.setText(replace2);
            }
        }else if(null != item.getIsHanTp() && null != item.getIsHanWz() &&
                item.getIsHanTp().equals("1") && item.getIsHanWz().equals("0")){//只有图片
            talk_msg_bigimg.setVisibility(View.VISIBLE);
            talk_msg_content.setVisibility(View.GONE);
            Glide.with(mContext).load(item.getPicUrl())
                    .into(talk_msg_bigimg);
        } else {
            talk_msg_bigimg.setVisibility(View.GONE);
            talk_msg_content.setVisibility(View.VISIBLE);
            if (replace2.contains("href=")) {
//            String s = "新版本内测免费送金币" +
//                    "<font ><a href= \"http://www.x1f6f.cn/tongchengyue.apk\"> 点击下载 </a ></font>";
                talk_msg_content.setText(getClickableHtml(mContext, replace2));
                talk_msg_content.setMovementMethod(LinkMovementMethod.getInstance());
            } else {
                String content2 = replace2.replace("\n", " ");
                if (content2.length() > 100) {
                    content2 = content2.substring(0, 100);
                }
                talk_msg_content.setText(new SpannableString(AndroidEmoji.ensure(content2)));
            }
        }
    }

    /**
     * 格式化超链接文本内容并设置点击处理
     * */
    public static CharSequence getClickableHtml(Context context, String html) {
        Spanned spannedHtml = Html.fromHtml(html);
        SpannableStringBuilder clickableHtmlBuilder = new SpannableStringBuilder(spannedHtml);
        URLSpan[] urls = clickableHtmlBuilder.getSpans(0, spannedHtml.length(), URLSpan.class);
        for(final URLSpan span : urls) {
            setLinkClickable(context,clickableHtmlBuilder, span);
        }
        return clickableHtmlBuilder;
    }

    /**
     * 设置点击超链接对应的处理内容
     * */
    public static void setLinkClickable(final Context context, final SpannableStringBuilder clickableHtmlBuilder, final URLSpan urlSpan) {
        int start = clickableHtmlBuilder.getSpanStart(urlSpan);
        int end = clickableHtmlBuilder.getSpanEnd(urlSpan);
        int flags = clickableHtmlBuilder.getSpanFlags(urlSpan);
        ClicksSpans clickableSpan = new ClicksSpans() {
            @Override
            public void onClick(View view) {
                WebViewActivity.openH5Activity(context,"",urlSpan.getURL());
            }
        };
        //实际使用过程中，可根据需要清空之前的span，防止出现重复调用的情况
        clickableHtmlBuilder.setSpan(clickableSpan, start, end, flags);
    }
}
